var wechat = {
  token: "92d99c2688ce85a8",

  setItem: function(key, value) {
    if(typeof value === "object") {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
    return true
  },

  getItem: function(key) {
    var value = localStorage.getItem(key)
    if(!value) return false
    try {
      return JSON.parse(value)
    } catch (error) {
      return value
    }
  },

  removeItem: function(key) {
    return localStorage.removeItem(key)
  },

  // 获取微信授权接口
  initWX: function() {
    tools.ajax({
      url: `/index/index/js_api`,
      data: {
        url: location.href
      },
      success: function(res) {
        wx.config(JSON.parse(res))
      }
    })
  },

  hide: function() {
    wx.ready(function() {
      wx.hideMenuItems({
        menuList: [   // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
          "menuItem:share:appMessage",
          "menuItem:share:timeline",
          "menuItem:share:qq",
          "menuItem:share:weiboApp",
          "menuItem:favorite",
          "menuItem:share:facebook",
          "menuItem:share:QZone",
          "menuItem:copyUrl",
          "menuItem:originPage",
          "menuItem:openWithQQBrowser",
          "menuItem:openWithSafari",
          "menuItem:share:email",
          "menuItem:share:brand",
          "menuItem:readMode",
          "menuItem:delete"
        ]
      });
    })
  },

  /**
   * 获取用户微信信息
   * @param {*} success  后台code为0时的回调
   * @param {*} error   后台code为1时的回调
   */
  getWXInfo: function() {
    tools.ajax({
      url: '/index/index/oauth_callback',
      success: function(res) {
        if(res.code === 0) {
          wechat.setItem('userInfo', res.data)
        } else if(res.code === 1) {
          wechat.removeItem('userInfo')
          wechat.wxLogin()
        }
      }
    })
  },

  /**
   * 微信登录
   * @param {*} url back的url
   */
  wxLogin: function(url) {
    tools.ajax({
      url: '/index/index/oauth',
      data: { url: location.href },
      success: function(res) {
        if(res.code === 0) {
          window.location.href = res.redirect_url
        }
      }
    })
  },

  /**
   * 微信授权登录
   */
  register: function(code) {
    tools.ajax({
      url: '/index/index/user',
      data: { code: code },
      success: function(res) {
        if(res.code === 0) {
          wechat.setItem('userInfo', res.data)
        } else if(res.code === 403) {
          wechat.removeItem('userInfo')
          wechat.wxLogin()
        }
      }
    })
  },

  share: function(options) {
    var option = $.extend(true, {}, {
      title: "天空文化传播",
      desc: "全城有礼游戏 区域广告宣传",
      link: location.href.replace(/code=.*\&state=.*\&?/, ""),
      imgUrl: '../../static/AdminLTE/img/avatar.gif',
      success: function() {}
    }, options)
    wx.onMenuShareAppMessage(option);
    wx.onMenuShareTimeline(option);
  },

  /**
   * 分享到朋友圈（即将废弃）
   * @param {*} options 
   */
  shareTimeline: function(options) {
    var option = $.extend(true, {}, {
      title: "天空文化传播",
      link: location.href.replace(/code=.*\&state=.*\&?/, ""),
      imgUrl: '../../static/AdminLTE/img/avatar.gif',
      success: function() {}
    }, options)
    wx.onMenuShareTimeline(option)
  },

  /**
   * 分享给好友（即将废弃）
   * @param {*} options 
   */
  shareAppMessage: function(options) {
    var option = $.extend(true, {}, {
      title: "天空文化传播",
      desc: "全城有礼游戏 区域广告宣传",
      link: location.href.replace(/code=.*\&state=.*\&?/, ""),
      imgUrl: '../../static/AdminLTE/img/avatar.gif',
      success: function() {}
    }, options)
    wx.onMenuShareAppMessage(option);
  }
}

// 初始化微信接口
wechat.initWX()

if(tools.getQueryString('code') && !wechat.getItem("userInfo")) {
  // 当 url 中有 code 时，走 user 接口，获取信息
  wechat.register(tools.getQueryString('code'))
} else {
  // wechat.getWXInfo()
}
